package com.justin.demo;

import com.justin.demo.utils.LogUtils;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * synchronized 同步锁
 */
public class SellTickets004 implements Runnable {

    //初始化票数
    private static int tickets = 30 ;

    private Lock lock = new ReentrantLock() ;

    @Override
    public void run() {
        while(true) {
            lock.lock();
            if (tickets > 0) {
                try {
                    //卖票延迟时间
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LogUtils.info("线程[{}] 正在卖第[{}]张票", Thread.currentThread().getName(), tickets);
                tickets--;
            }
            lock.unlock();
        }
    }
}
